<?php
require_once(ROOTDIR."/include/database/class.UserQueries.php");
require_once(ROOTDIR."/include/database/class.ServerQueries.php");

$userQueries = new UserQueries();
$serverQueries = new ServerQueries();

?>
<script src="javascript/ajax.js" language="javascript" type="text/javascript"></script>
<script src="javascript/functions.js" language="javascript" type="text/javascript"></script>
<script type="text/javascript">
function deleteVerify(id, name) {
  if(confirm("Do you really want to delete "+name+"?")) {
    document.getElementById("deleteUser"+id).submit();
  }
}
</script>
<?php

$error = false;
$newAdminError = false;

$nopost = true; // Flag of whether the form was submitted yet
if(isset($_POST['nopost'])) {
	$nopost = $_POST['nopost'];
}

// Boolean values of whether post values are valid for new admin
$valid = array("username"=>false,
        "steamId"=>false,
        "email"=>true);

// If this is set, then that means a new admin is being added
if(isset($_POST['submitAdd'])) {
  if($config->enableSmfIntegration) {
    $id = $_POST['username'];
    // Steam ID
    if(isset($_POST['steamId'])) {
    	$steamId = $_POST['steamId'];
    	if(!empty($steamId)) {
    	 if(preg_match("/^STEAM_[01]:[01]:\d{0,10}$/", $steamId)) {
    		  $valid['steamId'] = true;
    		}
    	}
    }
    
    if($valid['steamId']) {
      $userQueries->addSmfUser($id, $steamId);
      $steamId = "";
    }
  } else {
    // User name
    if(isset($_POST['username'])) {
    	$username = $_POST['username'];
    	if(!$userQueries->usernameExist($username) && !empty($username)) {
    		$valid['username'] = true;
    	}
    }

    // Steam ID
    if(isset($_POST['steamId'])) {
    	$steamId = $_POST['steamId'];
    	if(!empty($steamId)) {
    	 if(preg_match("/^STEAM_[01]:[01]:\d{0,10}$/", $steamId)) {
    		  $valid['steamId'] = true;
    		}
    	}
    }

    if(isset($_POST['email'])) {
    	$email = $_POST['email'];
    	if(!$userQueries->emailExist($email) && !empty($email)) {
    		if(!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $email)) {
    			$valid['email'] = false;
    		}
    	}
    }

    // Only a username and steam id are required for this
    if($valid['username'] && $valid['steamId']) {
      $userQueries->addUser($username, $userQueries->createRandomPassword(), $_POST['userAccessLevel'], $steamId, $email);
      $username = "";
      $steamId = "";
      $email = "";
    } else {
      $newAdminError = true;
    }
  }
}

// If a server is being deleted
if(isset($_POST['submitDelete'])) {
  if(!$userQueries->deleteUser($_POST['userId'])) {
    $deleteError = true;
  }
}

// Resetting a person's password
if(isset($_POST['forgotPassword'])) {
  $userQueries->forgotPassword($_POST['email']);
}

// Get list of users
$users = $userQueries->getUsers();
?>

<?php
// Only those with full privs can edit users
if($fullPower) {

  if($deleteError) {
    ?><p class="error">Not allowed to delete the last super-user</p><?php
  }
?>
<div class="tborder">
  <div id="tableHead">
    <div><b>Admin List</b></div>
  </div>
  <table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
    <tr>
      <th class="colColor1" nowrap>Name</th>
      <th class="colColor2" nowrap>Access Level</th>
      <th class="colColor1" nowrap>Email</th>
      <th class="colColor2" nowrap>Steam ID</th>
      <th class="colColor1" nowrap>Save</th>
      <th class="colColor2" nowrap>Delete</th>
      <?php
      if(!$config->enableSmfIntegration) {
      ?>
      <th class="colColor1" nowrap></th>
      <?php } ?>
    </tr>
    <?php foreach($users as $user) {
          ?>
          <tr>
            <?php if(!$config->enableSmfIntegration) { ?>
            <td class="colColor1" nowrap><input type="text" id="username:<?=$user->getId()?>" name="username:<?=$user->getId()?>" value="<?=$user->getName()?>" size="40" maxlength="40"/></td>
            <?php }
            else { ?>
            <td class="colColor1" nowrap><input type="hidden" id="username:<?=$user->getId()?>" name="username:<?=$user->getId()?>" value="<?=$user->getName()?>"/><?=$user->getName()?></td>
            <?php } ?>
            <td class="colColor2" nowrap>
              <select id="userAccessLevel:<?=$user->getId()?>" name="userAccessLevel:<?=$user->getId()?>">
                <option value="1" <?php if($user->getAccessLevel() == 1) { echo "selected"; } ?>>Super User</option>
                <option value="2" <?php if($user->getAccessLevel() == 2) { echo "selected"; } ?>>Ban Manager</option>
                <option value="3" <?php if($user->getAccessLevel() == 3) { echo "selected"; } ?>>Admin</option>
                <option value="4" <?php if($user->getAccessLevel() == 4) { echo "selected"; } ?>>Member</option>
              </select>
            </td>
            <?php if(!$config->enableSmfIntegration) { ?>
            <td class="colColor1" nowrap><input type="text" id="userEmail:<?=$user->getId()?>" name="userEmail:<?=$user->getId()?>" value="<?=$user->getEmail()?>" size="35" maxlength="80"/></td>
            <?php }
            else { ?>
            <td class="colColor1" nowrap><input type="hidden" id="userEmail:<?=$user->getId()?>" name="userEmail:<?=$user->getId()?>" value="<?=$user->getEmail()?>"/><?=$user->getEmail()?></td>
            <?php } ?>
            <td class="colColor2" nowrap><input type="text" id="userSteamId:<?=$user->getId()?>"name="userSteamId:<?=$user->getId()?>" value="<?=$user->getSteamId()?>" size="25" maxlength="25"/></td>
            <td id="save:<?=$user->getId()?>" class="colColor1" onclick="saveUser('<?=$user->getId()?>');" style="cursor:pointer;">
            <img src="images/tick.png"/>
            </td>
            <td class="colColor2" style="cursor:pointer;" onclick="deleteVerify('<?=$user->getId()?>', '<?=$user->getName()?>');">
            <form action="index.php?page=manageAdmins" id="deleteUser<?=$user->getId()?>" name="deleteUser<?=$user->getId()?>" method="POST">
              <input type="hidden" name="userId" id="userId" value="<?=$user->getId()?>"/>
              <input type="hidden" name="submitDelete" value="1">
              <img src="images/cross.png"/>
            </form>
            </td>
            <?php
            if(!$config->enableSmfIntegration) {
            ?>
            <td class="colColor1">
              <form action="index.php?page=manageAdmins" method="post"> 
                <input type="hidden" name="email" size="60" maxlength="80" value="<?=$user->getEmail()?>"/>
                <input type="submit" name="forgotPassword" value="Reset Password" class="button" style="cursor:pointer;" onclick="alert('Password reset for <?=$user->getName()?>')">
              </form>            
            </td>
            <?php } ?>
          </tr>
    <?php } ?>
    
    <tr>
      <td class="colColor1" nowrap>&nbsp;</td>
      <td class="colColor2" nowrap></td>
      <td class="colColor1" nowrap></td>
      <td class="colColor2" nowrap></td>
      <td class="colColor1" nowrap></td>
      <td class="colColor2" nowrap></td>
      <?php
      if(!$config->enableSmfIntegration) {
      ?>
      <td class="colColor1" nowrap></td>
      <?php } ?>
    </tr>
  </table>
  <div id="tableBottom">
    <div>
      <?php
        $timeToProcess = (8 + count($users)*2);
      ?>
			<input type="button" value="Save Admins to Servers" class="button" onclick="location.href='index.php?page=uploadAdmins'" /> <b>Estimated time to process: <?=$timeToProcess?> seconds</b>
    </div>
  </div>
    
</div>
<h5>Resetting a password emails the user a new randomly generated password.</h5>
<br/>
<div class="tborder">
  <div id="tableHead">
    <div><b>Add New Admin</b></div>
  </div>
  <table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
  <form action="index.php?page=manageAdmins" method="post" id="form">
  	<table class="bordercolor" width="100%" cellspacing="1" cellpadding="5" border="0" style="margin-top: 1px;">
      <?php
      if(!$config->enableSmfIntegration) {
      ?>
      <tr>
  			<td class="rowColor1">*Username:</td>
  			<td class="rowColor1"><input type="text" name="username" value="<?=$username?>" size="40" maxlength="40" />
  			<?php if(!$valid['username'] && !$nopost) { ?><span class="error">Username already taken</span><?php } ?></td>
  		</tr>
  		<?php
      } else {
      ?>
      <tr>
        <td class="rowColor1">*Username:</td>
  			<td class="rowColor1">
          <select name="username">
            <option value=""></option>
            <?php
            $smfUsers = $userQueries->getSMFUsers();
            foreach($smfUsers as $smfUser) {
              ?><option value="<?=$smfUser->getId()?>"><?=$smfUser->getName()?></option><?php
            }
            ?>
          </select>
  			</td>
      </tr>
      <?php
      }
      ?>
  		<tr>
  			<td class="rowColor2">*Steam ID:</td>			
  			<td class="rowColor2"><input name="steamId" id="steamdId" type="text" value="<?=$steamId?>" size="25" maxlength="25"/> (must be in <b>STEAM_X:X:XXXXXX</b> format)
  			<?php if(!$valid['steamId'] && !$nopost) { ?><span class="error">Steam ID not in vaild format</span><?php } ?></td>
  		</tr>
  		<?php
      if(!$config->enableSmfIntegration) {
      ?>
  		<tr>
  			<td class="rowColor1">Email:</td>
  			<td class="rowColor1"><input type="text" name="email" value="<?php if(!empty($email)) { echo $email; } ?>" size="60" maxlength="80" />
  			<?php if(!$valid['email'] && !$nopost) { ?><span class="error">Enter a valid email</span><?php } ?></td>
  		</tr>
  		<tr>
  			<td class="rowColor2">Access Level:</td>
  			<td class="rowColor2">
  			 <select id="userAccessLevel" name="userAccessLevel">
            <option value="1">Super User</option>
            <option value="2">Ban Manager</option>
            <option value="3">Admin</option>
            <option value="4" selected>Member</option>
          </select>
        </td>
  		</tr>
  		<?php
      }
      ?>
  		<tr>
  			<td align="left" colspan="3" class="rowColor1">
  				<input type="hidden" name="nopost" value="0" />
  				<input type="reset" value="Reset Form" class="button" />&nbsp;
  				<input type="submit" name="submitAdd" value="Submit" class="button" /></td>
  		</tr>
  </table>
</div>
<h5>* Is a required field when adding a new admin</h5>
<?php
} else {
?>
<div class="tborder">
  <div id="tableHead">
    <div><b>Access Denied</b></div>
  </div>
<div class="tborder">
<?php
}
?>
